<?php
declare(strict_types=1);

namespace app\common\model\delivery;

use app\common\library\Pay;
use think\facade\Db;
use app\common\model\base\BaseModel;
use app\common\model\base\ConstTraits;

class ChannelOrder extends BaseModel
{
    use ConstTraits;

    const ORDER_TYPE=[
        '1'=>'用户下单',
        '2'=>'代客下单',
        '3'=>'自动下单',
    ];

    const PAY_TYPE=[
        'wechat'=>'微信支付',
        'delivery'=>'货到付款',
        'month'=>'账单月结',
        'coupon'=>'商品券支付',
        'card'=>'会员卡余额支付'
    ];

    public function driver()
    {
        return $this->hasOne(Driver::class,'id','driver_id');
    }

    public function customer()
    {
        return $this->hasOne(Customer::class,'id','customer_id');
    }

    public function goods()
    {
        return $this->hasOne(Goods::class,'id','goods_id');
    }

    public function spec()
    {
        return $this->hasOne(GoodsSpec::class,'id','spec_id');
    }

    public function channel()
    {
        return $this->hasOne(Channel::class,'id','channel_id');
    }

    public function address()
    {
        return $this->hasOne(CustomerAddress::class,'id','address_id');
    }

    public function cancelSuccess($number,$remark)
    {
        if($this->receive_time){
            throw new \Exception('该订单已经送达,禁止退还');
        }
        if($this->refuse_number+$number>$this->number){
            throw new \Exception('取消的数量过多');
        }
        $channel=Channel::find($this->channel_id);
        if($this->pay_type=='wechat'){
            $total_fee=floatval($this->price*$this->number);
            $refund_fee=floatval($this->price*$number);
            $r=(new Pay($channel->sub_mch_id))->refund($this->order_no,$total_fee,$refund_fee);
            if(!isset($r['status']) || $r['status']!='PROCESSING'){
                throw new \Exception($r['message']);
            }
        }
        Db::startTrans();
        try{
            $unit='';
            foreach ($channel->goods as $goods){
                if($goods['id']==$this->goods_id){
                    foreach ($goods['spec'] as $spec){
                        if($spec['id']==$this->spec_id){
                            $unit=$spec['unit'];
                            break;
                        }
                    }
                }
            }
            $xremark=[];
            if($this->refuse_remark_id){
                $xremark=Text::get($this->refuse_remark_id);
            }
            $xremark[]=[
                'time'=>date('Y-m-d H:i:s'),
                'number'=>$number.$unit,
                'message'=>$remark,
            ];
            $remark_id=Text::add($xremark,'channel_order');
            if($this->refuse_number+$number==$this->number){
                $this->is_cancel=1;
            }
            $this->refuse_number=$this->refuse_number+$number;
            $this->refuse_remark=$remark_id;
            $this->total_money=($this->number-$this->refuse_number)*$this->price;
            $this->save();
            //商品券的退还
            if($this->pay_type=='coupon'){
                $verify=CouponVerify::with(['coupon','couponlist'])->where(['order_table'=>'channel_order','order_id'=>$this->id])->find();
                if($this->refuse_number+$number==$this->number){
                    $coupon=$verify->coupon;
                    $coupon->take=$coupon->take-1;
                    $coupon->save();
                }
                $couponlist=$verify->couponlist;
                $couponlist->take=$couponlist->take-$number;
                $couponlist->save();
            }
            //会员卡余额退还
            if($this->pay_type=='card'){
                $verify=CouponVerify::with(['coupon','couponlist'])->where(['order_table'=>'channel_order','order_id'=>$this->id])->find();
                if($this->refuse_number+$number==$this->number){
                    $coupon=$verify->coupon;
                    $coupon->take=$coupon->take-1;
                    $coupon->save();
                }
                $couponlist=$verify->couponlist;
                $couponlist->balance=$couponlist->balance+$verify->money;
                $couponlist->save();
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            throw new \Exception($e->getMessage());
        }
    }

    public function receive()
    {
        $time=time();
        $this->receive_time=date('Y-m-d H:i:s',$time);
        $this->delivery_day=date('Y-m-d',$time);
        $this->save();
        SellerCommission::commissionOrder($this);
        if($this->pay_money>0 && $this->user_id){
            UserLog::addScoreLog($this->user_id,'add',$this->pay_money,$this->order_no,'下单增加积分');
        }
    }
}
